package tt.dz.biz.service.user;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.user.OperListDAO;
import tt.dz.common.BusinessException;
import tt.dz.common.DZStringUtil;
import tt.dz.common.DateUtil;
import tt.dz.common.DzDigester;
import tt.dz.common.DzErrorCode;
import tt.dz.common.DzErrorCode.ExceptionErrorCode;
import tt.dz.common.LogUtil;
import tt.dz.vo.OperListVO;

@Service("operListService")
public class OperListService
{
  @Resource(name="operListDAO")
  OperListDAO operListDAO;
  private static final String DEFAULT_PASSWORD = "123456";
  
  public List<OperListVO> listAllOperList()
  {
    List<OperListVO> operListList = null;
    try
    {
      operListList = this.operListDAO.listAllOperList();
    }
    catch (Exception e)
    {
      LogUtil.error("查询所有操作员异常", e);
      operListList = new ArrayList();
    }
    return operListList;
  }
  
  public List<OperListVO> queryOperList(OperListVO operListVO)
  {
    List<OperListVO> operListList = null;
    try
    {
      operListList = this.operListDAO.queryOperList(operListVO);
    }
    catch (Exception e)
    {
      LogUtil.error("根据传入条件查询操作员异常--入参：" + operListVO.toString(), e);
      operListList = new ArrayList();
    }
    return operListList;
  }
  
  public List<OperListVO> queryOperListNotLike(OperListVO operListVO)
  {
    List<OperListVO> operListList = null;
    try
    {
      operListList = this.operListDAO.queryOperListNotLike(operListVO);
    }
    catch (Exception e)
    {
      LogUtil.error("根据传入条件查询操作员（不做like查询）异常,--入参：" + operListVO.toString(), e);
      operListList = new ArrayList();
    }
    return operListList;
  }
  
  public boolean insertOperList(OperListVO operListVO)
  {
    boolean excuteFlag = false;
    try
    {
      Date now = DateUtil.getDBCurrentTime();
      SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
      operListVO.setManagerId(format.format(now) + 
        DZStringUtil.getFixedLengthString(4, this.operListDAO.querySequence("oper_seq").toString()));
      
      String password = StringUtils.isBlank(operListVO.getPassword()) ? "123456" : operListVO.getPassword();
      operListVO.setPassword(DzDigester.sha1ForOper(password));
      operListVO.setRegistTime(now);
      int i = this.operListDAO.insertOperList(operListVO);
      excuteFlag = i > 0;
    }
    catch (Exception e)
    {
      LogUtil.error("插入操作员异常--入参：" + operListVO.toString(), e);
    }
    return excuteFlag;
  }
  
  public boolean updateOperList(OperListVO operListVO)
  {
    boolean excuteFlag = false;
    try
    {
      int i = this.operListDAO.updateOperList(operListVO);
      excuteFlag = i > 0;
    }
    catch (Exception e)
    {
      LogUtil.error("更新操作员异常--入参：" + operListVO.toString(), e);
      
      throw new BusinessException(DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.value(), DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.getDesc(), e.getMessage());
    }
    return excuteFlag;
  }
  
  public OperListVO validateOper(OperListVO operListVO)
  {
    OperListVO result = null;
    try
    {
      operListVO.setManagerId(this.operListDAO.getSystemCurrentOper());
      result = this.operListDAO.validateOper(operListVO);
    }
    catch (Exception e)
    {
      LogUtil.error("验证操作员异常--入参：" + operListVO.toString(), e);
      
      throw new BusinessException(DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.value(), DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.getDesc(), e.getMessage());
    }
    return result;
  }
  
  public List<OperListVO> queryOperListBySys(String sysCode, String startTime, String endTime)
  {
    List<OperListVO> operListList = null;
    try
    {
      operListList = this.operListDAO.queryOperListBySys(sysCode, startTime, endTime);
    }
    catch (Exception e)
    {
      LogUtil.error("根据syscode查询当前子系统下有权限的操作员 异常--入参+" + sysCode + "--" + startTime + "--" + endTime, e);
      operListList = new ArrayList();
      
      throw new BusinessException(DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.value(), DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.getDesc(), e.getMessage());
    }
    return operListList;
  }
  
  public OperListVO queryOper(String aMobile)
  {
    return this.operListDAO.queryOper(aMobile);
  }
  
  public List<OperListVO> queryOperForLogin(String aMobile)
    throws Exception
  {
    try
    {
      return this.operListDAO.queryOperForLogin(aMobile);
    }
    catch (Exception e)
    {
      LogUtil.error("操作员登录异常，参数为：aMobile=" + aMobile, e);
      throw e;
    }
  }
}
